<template>
  <div class="mileage-wave" :id="id"></div>
</template>

<script>
  import echarts from 'echarts/lib/echarts'
  import 'echarts/lib/chart/line'
  import 'echarts/lib/component/legend'
  import 'echarts/lib/component/tooltip'
  import { mapActions, mapState} from 'vuex'

  export default {
    props: {
      id: {
        type: String,
        required: true
      },
      xAxis: {
        type: Array,
        required: true,
      },
      data0: {
        type: Array,
        required: true,
      },
      data1: {
        type: Array,
        required: true,
      },
      data2: {
        type: Array,
        required: true,
      }
    },
    watch: {
      xAxis (val, oldval) {
        this.onReady()
      }
    },
    data () {
      return {
        title: '实时里程数',
        blueColor: '#47b8c8',
        legendData: ['去年同期里程', '上期里程', '本期里程'],
        dataColor: ['#918cff', '#04bcc6', '#1d80ff'],
        areaColor: ['#532bd7', '#08090d']
      }
    },
    mounted () {
      this.onReady()
    },
    methods: {
      onReady () {
        const { id, xAxis, data0, data1, data2, legendData, blueColor, dataColor, areaColor } = this
        const options = {
          legend: {
            icon: 'rect',
            data: legendData,
            textStyle: {
              color: blueColor,
              fontSize: 12,
            },
            itemWidth: 15,
            itemHeight: 10,
            left: '3%',
            bottom: '1%'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { type: 'shadow' },
            position: function (pos, params, dom, rect, size) {
              // 鼠标在左侧时 tooltip 显示到右侧，鼠标在右侧时 tooltip 显示到左侧。
              var obj = {top: 60}
              obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 5
              return obj
            },
            formatter: function (params) {
              let name = params[0].name
              let name0 = params[0].seriesName
              let name1 = params[1].seriesName
              let name2 = params[2].seriesName
              let value0 = params[0].value
              let value1 = params[1].value
              let value2 = params[2].value
              let dom = `${name}<br/>${name0}:${value0}万公里<br/>${name1}:${value1}万公里<br/>${name2}:${value2}万公里`
              return dom
            }
          },
          grid: {
            left: '3%',
            right: '4.5%',
            top: '9%',
            bottom: '16%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              boundaryGap: false,
              data: xAxis,
              axisLabel: {
                color: blueColor
              },
              axisLine: { show: false },
              axisTick: { show: false }
            }
          ],
          yAxis: [
            {
              type: 'value',
              axisLabel: {
                color: blueColor,
                formatter: (value, index) => {
                  return parseInt(value) + '万'
                }
              },
              min: value => {return value.min},
//              max: maxBasicVal,
              axisLine: { show: false }, // 取消边界线
              axisTick: { show: false }, // 取消数字轴上的刻度
              splitLine: { show: false } // 取消背景上的刻度
            }
          ],
          series: [
            {
              name: legendData[0],
              type: 'line',
              smooth: true,
              symbol: 'circle',
              lineStyle: {
                normal: {
                  color: dataColor[0]
                }
              },
              areaStyle: {
                normal: {
                  color: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                      offset: 0, color: '#675cff' // 0% 处的颜色
                    }, {
                      offset: 1, color: '#0a0b1a' // 100% 处的颜色
                    }],
                    globalCoord: false // 缺省为 false
                  }
                }
              },
              data: data0.map((item) => {
                return (item / 10000).toFixed(6)
              })
            },
            {
              name: legendData[1],
              type: 'line',
              smooth: true,
              symbol: 'circle',
              lineStyle: {
                normal: {
                  color: dataColor[1]
                }
              },
              areaStyle: {
                normal: {
                  color: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                      offset: 0, color: '#02d8e0' // 0% 处的颜色
                    }, {
                      offset: 1, color: '#0a0b1a' // 100% 处的颜色
                    }],
                    globalCoord: false // 缺省为 false
                  }
                }
              },
              data: data1.map((item) => {
                return (item / 10000).toFixed(6)
              })
            },
            {
              name: legendData[2],
              type: 'line',
              smooth: true,
              symbol: 'circle',
              lineStyle: {
                normal: {
                  color: dataColor[2]
                }
              },
              areaStyle: {
                normal: {
                  color: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                      offset: 0, color: '#3372ff' // 0% 处的颜色
                    }, {
                      offset: 1, color: '#0a0b1a' // 100% 处的颜色
                    }],
                    globalCoord: false // 缺省为 false
                  }
                }
              },
              data: data2.map((item) => {
                return (item / 10000).toFixed(6)
              })
            }
          ],
          color: dataColor
        }
        const myWave = echarts.init(document.getElementById(id))
        myWave.setOption(options, true)
        window.addEventListener('resize', function () {
          myWave.resize()
        })
      }
    }
  }
</script>

<style lang="css" scoped>
  .mileage-wave{
    width: 100%;
  }
</style>
